﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VQPPLAN
    {
        public void SetUpNewRow(ref DataRow dwReturn)
        {

            switch (app.company)
            {
                case "YGR":
                case "YCC":
                case "TST":
                default:
                    #region=========
                    dwReturn["QPPMANDANT"] = app.Mandant;
                    dwReturn["QPPPNRKRS"] = "  ";
                    dwReturn["QPPPLFDNR"] = "000000";
                    dwReturn["QPPAENZL"] = "0";
                    dwReturn["QPPAENB"] = "        ";
                    dwReturn["QPPAEND"] = "0";
                    dwReturn["QPPNEUB"] = "        ";
                    dwReturn["QPPNEUD"] = "0";
                    dwReturn["QPPSPEZIF"] = "                                                  ";
                    dwReturn["QPPDATVON"] = "0";
                    dwReturn["QPPDATBIS"] = "0";
                    dwReturn["QPPSPGRD"] = "  ";
                    dwReturn["QPPSPSTA"] = " ";
                    dwReturn["QPPARTST"] = " ";
                    dwReturn["QPPPHASE"] = "PB";
                    dwReturn["QPPGKREGEL"] = "0";
                    dwReturn["QPPPRVORS"] = "        ";
                    dwReturn["QPPVERSAE"] = " ";
                    dwReturn["QPPVERSZL"] = "0";
                    dwReturn["QPPFILLER"] = "                                                       ";
                    #endregion
                    break;
            }
        }
        public DataRow Convert2QPPLAN(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryQPPLAN(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
                    dwReturn["QPPMANDANT"] = " ".PadRight(2);
                    dwReturn["QPPPNRKRS"] = " ".PadRight(2);
                    dwReturn["QPPPLFDNR"] = " ".PadRight(6);
                    dwReturn["QPPAENZL"] = " ".PadRight(0);
                    dwReturn["QPPAENB"] = " ".PadRight(8);
                    dwReturn["QPPAEND"] = " ".PadRight(0);
                    dwReturn["QPPNEUB"] = " ".PadRight(8);
                    dwReturn["QPPNEUD"] = " ".PadRight(0);
                    dwReturn["QPPSPEZIF"] = " ".PadRight(50);
                    dwReturn["QPPDATVON"] = " ".PadRight(0);
                    dwReturn["QPPDATBIS"] = " ".PadRight(0);
                    dwReturn["QPPSPGRD"] = " ".PadRight(2);
                    dwReturn["QPPSPSTA"] = " ".PadRight(1);
                    dwReturn["QPPARTST"] = " ".PadRight(1);
                    dwReturn["QPPPHASE"] = " ".PadRight(2);
                    dwReturn["QPPGKREGEL"] = " ".PadRight(0);
                    dwReturn["QPPPRVORS"] = " ".PadRight(8);
                    dwReturn["QPPVERSAE"] = " ".PadRight(1);
                    dwReturn["QPPVERSZL"] = " ".PadRight(0);
                    dwReturn["QPPFILLER"] = " ".PadRight(55);
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapQPPLAN(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveQPPLAN(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryQPPLAN(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapQPPLAN(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertQPPLAN(dwLocal, ITXDBVERSION);
            }
            UpdateQPPLAN(dwLocal);
        }
        public void InsertQPPLAN(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                default:
                case "JLT":
                    or1.CommandText = "INSERT INTO QPPLAN ( QPPMANDANT,QPPPNRKRS,QPPPLFDNR,QPPAENZL,QPPAENB,QPPAEND,QPPNEUB,QPPNEUD,QPPSPEZIF,QPPDATVON,QPPDATBIS,QPPSPGRD,QPPSPSTA,QPPARTST,QPPPHASE,QPPGKREGEL,QPPPRVORS,QPPVERSAE,QPPVERSZL,QPPFILLER)"
                    + " VALUES (:QPPMANDANT:,:QPPPNRKRS:,:QPPPLFDNR:,:QPPAENZL:,:QPPAENB:,:QPPAEND:,:QPPNEUB:,:QPPNEUD:,:QPPSPEZIF:,:QPPDATVON:,:QPPDATBIS:,:QPPSPGRD:,:QPPSPSTA:,:QPPARTST:,:QPPPHASE:,:QPPGKREGEL:,:QPPPRVORS:,:QPPVERSAE:,:QPPVERSZL:,:QPPFILLER:)";
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    break;
                case "YGR":
                    or1.CommandText = "INSERT INTO QPPLAN ( QPPMANDANT,QPPPNRKRS,QPPPLFDNR,QPPAENZL,QPPAENB,QPPAEND,QPPNEUB,QPPNEUD,QPPSPEZIF,QPPDATVON,QPPDATBIS,QPPSPGRD,QPPSPSTA,QPPARTST,QPPPHASE,QPPGKREGEL,QPPPRVORS,QPPVERSAE,QPPVERSZL,QPPFILLER)"
                   + " VALUES (:QPPMANDANT:,:QPPPNRKRS:,:QPPPLFDNR:,:QPPAENZL:,:QPPAENB:,:QPPAEND:,:QPPNEUB:,:QPPNEUD:,:QPPSPEZIF:,:QPPDATVON:,:QPPDATBIS:,:QPPSPGRD:,:QPPSPSTA:,:QPPARTST:,:QPPPHASE:,:QPPGKREGEL:,:QPPPRVORS:,:QPPVERSAE:,:QPPVERSZL:,:QPPFILLER:)";
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }

            Connect2Proxy(or1);
        }
        public DataSet QueryQPPLAN(DataRow dwData, int option = 1)
        {
            DataSet SResult = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "SELECT* FROM QPPLAN"
                        + " WHERE  QPPSPGRD='  ' AND QPPPNRKRS=:QPPPNRKRS: AND QPPPLFDNR=:QPPPLFDNR:"
                           + " ORDER BY 1,2,3";
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    break;
                case 2:
                    or1.CommandText = "SELECT* FROM QPPLAN  WHERE   QPPSPGRD='  ' AND QPPMANDANT=:QPPMANDANT:"
                        + " ORDER BY 1,2,3";
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    break;
                case 3:
                    or1.CommandText = "SELECT * FROM QPPLAN  "
                    + " WHERE   QPPSPGRD='  ' AND QPPMANDANT=:QPPMANDANT: AND QPPARTST=:QPPARTST: AND QPPPNRKRS LIKE '"+dwData["QPPPNRKRS"].ToString().Trim()+"%' AND QPPPLFDNR LIKE '" + dwData["QPPPLFDNR"].ToString().Trim() + "%' AND QPPSPEZIF LIKE '%" + dwData["QPPSPEZIF"].ToString().Trim() + "%'"
                        + " ORDER BY 1,2,3";
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    break;
                case -1:
                    or1.CommandText = "SELECT QPPPNRKRS,QPPPLFDNR,QPPPNRKRS||QPPPLFDNR AS KEY,QPPSPEZIF,QPPARTST, QPPSPEZIF AS DES FROM QPPLAN  WHERE   QPPSPGRD='  ' AND QPPMANDANT=:QPPMANDANT: AND QPPARTST LIKE '"+dwData["QPPARTST"].ToString()+"%' AND QPPPNRKRS LIKE '"+dwData["QPPPNRKRS"].ToString()+ "%'"
                    + " ORDER BY 1,2,3";
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    break;

            }

            SResult = Connect2Proxy(or1);
            if (SResult.Tables.Count > 0)
            {
                SResult.Tables[0].TableName = "QPPLAN";
            }
            return SResult;
        }
        public void DeleteQPPLAN(DataRow dwData, int option = 1)
        {

            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM QPPLAN  "
                           + " WHERE   QPPMANDANT=:QPPMANDANT: AND QPPPNRKRS=:QPPPNRKRS: AND QPPPLFDNR =:QPPPLFDNR: ";
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    break;
            }
            Connect2Proxy(or1);
        }

        public void UpdateQPPLAN(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {

            or1 = OracleDB.CreateCommand();
            switch (option)
            {

                case 0://JLT
                    or1.CommandText = "UPDATE QPPLAN SET QPPMANDANT=:QPPMANDANT:,QPPPNRKRS=:QPPPNRKRS:,QPPPLFDNR=:QPPPLFDNR:,QPPAENZL=:QPPAENZL:,QPPAENB=:QPPAENB:,QPPAEND=:QPPAEND:,QPPNEUB=:QPPNEUB:,QPPNEUD=:QPPNEUD:,QPPSPEZIF=:QPPSPEZIF:,QPPDATVON=:QPPDATVON:,QPPDATBIS=:QPPDATBIS:,QPPSPGRD=:QPPSPGRD:,QPPSPSTA=:QPPSPSTA:,QPPARTST=:QPPARTST:,QPPPHASE=:QPPPHASE:,QPPGKREGEL=:QPPGKREGEL:,QPPPRVORS=:QPPPRVORS:,QPPVERSAE=:QPPVERSAE:,QPPVERSZL=:QPPVERSZL:,QPPFILLER=:QPPFILLER:"
                           + " WHERE   QPPMANDANT=:QPPMANDANT: AND QPPPNRKRS=:QPPPNRKRS: AND QPPPLFDNR =:QPPPLFDNR: ";
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    break;
                default:
                    break;
            }

            Connect2Proxy(or1);
        }
    }
}
